#! /bin/bash
# Custom Bru LTO tape copy script
# Uses tapecopy script supplied by BRU

PATH=$PATH:/bru
export PATH

. brufunclib

BLOCKSIZE_K=1024
JOB_START_SECS="$(date +%s)"
BACKUP_SIZE_KB=0

echo ""
echo "****************** BRU LTO Copy Script ******************"
echo ""

fnCopyOperationProgress()
{
	line_num=1
	echo "Scanning SOURCE tape ..."
	echo ""
	while read line
	do
		echo "$line" | grep "BlockSize: 0" > /dev/null
		if [ $? -eq 0 ]; then
			if [ $line_num -eq 1 ]; then
				COPY_START_SECS="$(date +%s)"
			fi
			secs=$(echo -n "$(date +%s)")
			elapsed_secs=$(echo -n "$[$secs - $COPY_START_SECS]")	
			size_k=$(echo -n "$[$BLOCKSIZE_K * $line_num]")
			size_m=$(echo "scale=2; $size_k / 1024" | bc)		
			size_g=$(echo "scale=2; $size_m / 1024" | bc)
			pcent=$(echo "scale=1; $size_k * 100 / $TAPE_KB_WRITTEN" | bc)
			elapsed_time=$(date -ur $elapsed_secs +%H:%M:%S)
			mbps=$(echo "scale=1; $size_m / $[$elapsed_secs + 1]" | bc)
			tput dl
			tput cuu1
			echo "Copied $size_g Gb (${pcent}%) in $elapsed_time at $mbps Mbps"
			line_num=$(echo -n "$[$line_num + 1]")
		fi
	done
}

fnCopyTape()
{
	DEST_DRIVE=$1
	SRC_DRIVE=$[ ($1 + 1) % 2 ]

	fnGetCopyId

	echo ""
	echo "Rewinding tapes ..."
	fnRewindTape $SRC_DRIVE &
	fnRewindTape $DEST_DRIVE	
	$(wait)

	sleep 2 # Doesn't work without this - drive not ready in time (HACK!)

	echo ""
	echo "Executing BRU tapecopy script..."
	echo ""
	
	blocksize_bytes=$(echo -n "$[BLOCKSIZE_K * 1024]")
	echo "" | tapecopy -i ntape$SRC_DRIVE -o ntape$DEST_DRIVE -b $blocksize_bytes 2> /dev/null | fnCopyOperationProgress

	stop_secs=$(echo -n "$(date +%s)")

	elapsed_secs=$(echo -n "$[$stop_secs - $JOB_START_SECS]")	
	backup_time=$(date -ur $elapsed_secs +%H:%M:%S)

	echo ""
	echo "Tape copy took $backup_time"

	# Rewinding and ejecting source and destination tapes done as concurrent jobs - to save time

	echo ""
	echo "Rewinding tapes ..."
	fnRewindTape $SRC_DRIVE &
	fnRewindTape "$1"	
	$(wait)

	echo ""
	echo "Ejecting tapes ..."
	fnEjectTape $SRC_DRIVE  &
	fnEjectTape "$1"

	$(wait)

	echo ""
	fnUpdateStatusFile ${LABEL}$COPY_ID
	echo ""
	echo "DONE!"
	echo ""
	echo "[Please label DESTINATION tape: ${LABEL}$COPY_ID BRU $ARCHIVE_ID (${BLOCKSIZE_K}K)]"
	echo ""
	exit
}

fnMain()
{
	if [ $1 == "SOURCE" ]; then
		WRITE_PROTECTION="ON"
	elif [ $1 == "DESTINATION" ]; then
		WRITE_PROTECTION="OFF"
	fi

	echo ""
	echo -n "Please insert $1 tape into an available drive and press Enter, (or \"q\" to quit)"
	read INPUT
	echo $INPUT | grep "^q$" > /dev/null
	if [ $? -eq 0 ]; then
		echo ""
		exit
	else
		fnLoadTape 0 "TRUE" $WRITE_PROTECTION
	fi

	if [ $LOAD_STATUS == "DRIVE_EMPTY" ]; then
		fnMain "SOURCE"
		return 0
	fi

	if [ $LOAD_STATUS == "TAPE_NOT_PROTECTED" ] && [ $1 == "SOURCE" ]; then
		echo "SOURCE tape is NOT write protected. Please write protect tape before proceeding"
		echo ""
		echo "Ejecting $1 tape ..."
		fnEjectTape $CURRENT_DRIVE
		fnMain "SOURCE"
		return 0
	fi	

	if [ $LOAD_STATUS == "TAPE_PROTECTED" ] && [ $1 == "DESTINATION" ]; then
		echo "DESTINATION tape is write protected. Please remove write protection before proceeding"
		echo ""
		echo "Ejecting $1 tape ..."
		fnEjectTape $CURRENT_DRIVE
		fnMain "DESTINATION"
		return 0
	fi	

	fnCheckTapeLabel $CURRENT_DRIVE

	if [ $LABEL == "UNDEFINED" ] && [ $1 == "SOURCE" ]; then
		echo "SOURCE tape does not contain a BRU Backup. Please insert a tape containing a BRU Backup"
		echo ""
		echo "Ejecting $1 tape ..."
		fnEjectTape $CURRENT_DRIVE
		fnMain "SOURCE"
		return 0
	fi

	if [ $1 == "SOURCE" ]; then
		fnSetVarTAPE_KB_WRITTEN $LABEL
		if [ $? == 0 ]; then
			TAPE_GB_WRITTEN=$(echo "scale=2; $TAPE_KB_WRITTEN / 1048576" | bc)
			echo "($TAPE_GB_WRITTEN Gb data stored on tape)"
		else
			echo ""
			echo "Script Terminated"
			echo ""
			exit
		fi
	fi

	if [ $LABEL != "UNDEFINED" ] && [ $1 == "DESTINATION" ]; then
		VALID_INPUT="n"
		while [ $VALID_INPUT == "n" ]
		do
			echo -n "Do you want to overwrite the tape (y/n)?"
			read PROCEED
			proceed=$(echo $proceed | tr '[:upper:]' '[:lower:]')
			echo $PROCEED | grep "^[yn]$" > /dev/null
			if [ $? -eq 0 ]; then
				VALID_INPUT="y"
				if [ $PROCEED != "y" ]; then
					echo ""
					echo "Ejecting $1 tape ..."
					fnEjectTape $CURRENT_DRIVE 
					fnMain "DESTINATION"
					return 0			
				else
					fnCopyTape "$1" 
					return 0
				fi
			fi	
		done
	fi

	if [ $1 == "SOURCE" ]; then
		fnMain "DESTINATION"
	elif [ $1 == "DESTINATION" ]; then
		fnCopyTape $CURRENT_DRIVE
	fi
}


fnMain "SOURCE"
exit











